package com.gxa.community.admin.controller.goods;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxa.community.admin.service.goods.impl.GoodsLogServiceImpl;
import com.gxa.community.core.pojo.domain.goodsdo.GoodsLog;
import com.gxa.community.core.pojo.domain.goodsdo.GoodsTag;
import com.gxa.community.core.utils.R;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 商品库存表 前端控制器
 * </p>
 *
 * @author gxa
 * @since 2022-07-18
 */
@Validated
@RestController
@Slf4j
@RequestMapping("/goods/log")
public class GoodsLogController {
    @Autowired
    GoodsLogServiceImpl goodsLogService;
    /**
     * 入库出库日志记录
     * @param type 记录类型
     * @param page 分页
     * @param size 分页
     * @return R
     */
    @GetMapping("/query")
    public R query(@NotNull(message = "请求类型不能为空1或者2") Integer type,
                   @NotNull(message = "page不能为空") Integer page,
                   @NotNull(message = "size不能为空") Integer size){
        Page<GoodsLog> Page = new Page<>(page,size);
        QueryWrapper <GoodsLog> wrapper = new QueryWrapper<>();
        wrapper.eq("type",type);
        Page<GoodsLog> logPage = goodsLogService.page(Page, wrapper);
        return R.ok().put("data",logPage.getRecords()).put("cont",logPage.getTotal());
    }

    /**
     * 多条件查询
     * @param name 商品名称
     * @param start 开始时间
     * @param end 结束时间
     * @param order 订单号
     * @param type 操作类型
     * @param op 操作人
     * @param page 分页
     * @param size 分页
     * @return R
     */
    @GetMapping("/seek")
    public R seel(String name,
                  long start,
                  long end,
                  String order,
                  String type,
                  String op,
                  @NotNull(message = "page不能为空") Integer page,
                  @NotNull(message = "size不能为空") Integer size){
        QueryWrapper <GoodsLog> wrapper = new QueryWrapper<>();
        Page<GoodsLog> Page = new Page<>(page,size);
        int cont = 0;
        if (StringUtils.isNotBlank(name)){
            cont++;
            wrapper.likeRight("goods_name",name);
        }
        if (start!=-1&&end!=-1){
            cont++;
            wrapper.between("action_time",start,end);
        }
        if (StringUtils.isNotBlank(order)){
            cont++;
            wrapper.eq("order_no",order);
        }
        if (StringUtils.isNotBlank(type)){
            cont++;
            wrapper.eq("action_type",type);
        }
        if (StringUtils.isNotBlank(op)){
            cont++;
            wrapper.eq("admin_id",op);
        }
        log.info("库存搜索体条件有:={}",cont);
        if (cont==0){
            var page1 = goodsLogService.page(Page,null);
            return R.ok().put("data",page1.getRecords()).put("cont",page1.getTotal());
        }
        var page1 = goodsLogService.page(Page, wrapper);
        return R.ok().put("data",page1.getRecords()).put("cont",page1.getTotal());
    }


}

